package com.huluobo.a2204kotlinshopping.detail

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.ToastUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.adapter.CommonPagerAdapter
import com.huluobo.a2204kotlinshopping.databinding.ActivityDetailBinding
import com.huluobo.a2204kotlinshopping.utils.ImageUtils
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseMVVMActivity
import org.w3c.dom.Comment

class DetailActivity : BaseMVVMActivity<ActivityDetailBinding, DetailViewModel>() {
    private val tabEntity = arrayListOf<CustomTabEntity>()
    private lateinit var commonPagerAdapter: CommonPagerAdapter
    private val fragments = mutableListOf<Fragment>()
    private var goodsId = 0
    private var imageUrl = ""

    override fun initData() {
        goodsId = intent.getIntExtra(Const.PARAM_GOODS_ID, 0)
        viewModel.getGoodsDetail(goodsId)

        viewModel.getDetailSuccess.observe(this) {
            if (it.code == 200) {
                dataBinding.goods = it.data
                viewModel.goodBean.value = it.data
                imageUrl = it.data.bannerList[0]
            } else {
                ToastUtils.showLong(it.message)
            }

        }

        viewModel.getDetailFailed.observe(this) {
            Log.i("TagA", "获取商品详情失败:$it")
        }

        viewModel.addToCartSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            //创建添加购物车动画的ImageView
            val cartImageView = ImageView(this)
            //代码创建View没有宽高,需要手动添加一个宽高,并且,不能直接给View添加宽高,需要用到LayoutParams来添加宽高
            val params = ConstraintLayout.LayoutParams(1000, 1000)
            //给imageView设置宽高
            cartImageView.layoutParams = params

            //让布局中加载出imageView
            dataBinding.detailRoot.addView(cartImageView)
            //给imageView添加图片资源
            ImageUtils.loadImage(this, imageUrl, cartImageView)

            //纵向平移动画
            val translationY = ObjectAnimator.ofFloat(
                cartImageView,
                "translationY",
                1500f
            )

            //横向平移动画
            val translationX = ObjectAnimator.ofFloat(
                cartImageView,
                "translationX",
                -400f
            )

            //横向缩放
            val scaleX = ObjectAnimator.ofFloat(
                cartImageView,
                "scaleX",
                1f, 0f
            )

            //纵向缩放
            val scaleY = ObjectAnimator.ofFloat(
                cartImageView,
                "scaleY",
                1f, 0f
            )

            //动画集合,一起执行以上动画
            val animatorSet = AnimatorSet()
            animatorSet.duration = 1000
            animatorSet.playTogether(translationY, translationX, scaleX, scaleY)
            animatorSet.start()

            animatorSet.addListener(object : AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                }

                override fun onAnimationEnd(animation: Animator) {
                    //当动画结束时,移除掉imageView
                    dataBinding.detailRoot.removeView(cartImageView)
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })

            viewModel.addToCartFailed.observe(this) {
                Log.i("TagA", "添加购物车失败:$it")
            }
        }
    }

    override fun initView() {
        tabEntity.clear()
        tabEntity.add(TabEntity("详情", 0, 0))
        tabEntity.add(TabEntity("评论", 0, 0))
        dataBinding.detailTabLayout.setTabData(tabEntity)

        fragments.clear()
        fragments.add(DetailFragment())
        fragments.add(CommentFragment())

        commonPagerAdapter = CommonPagerAdapter(this, fragments)
        dataBinding.detailVp2.adapter = commonPagerAdapter

        //tabLayout和vp2的联动
        dataBinding.detailTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                dataBinding.detailVp2.currentItem = position
            }

            override fun onTabReselect(position: Int) {
            }
        })

        //vp2和tabLayout的联动
        dataBinding.detailVp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                dataBinding.detailTabLayout.currentTab = position
            }
        })

        //添加购物车的触发
        dataBinding.detailAddToCart.setOnClickListener {
            viewModel.addToCart(goodsId, 1)
        }

        dataBinding.detailService.setOnClickListener {
            val intent = Intent(this, ServiceActivity::class.java)
            val bundle = Bundle()
            bundle.putString("conversationId", "kefu")
            bundle.putInt("chatType", 1)
            intent.putExtras(bundle)
            startActivity(intent)
        }
    }

    override fun getViewModel(): Class<DetailViewModel> {
        return DetailViewModel::class.java
    }

    override fun getLayoutId(): Int = R.layout.activity_detail
}

